// https://www.acwing.com/problem/content/849/

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

const int MAX_NODES = 100010;

vector<int> G[MAX_NODES];
int m, n;

int bfs() {
    queue<int> Q;
    vector<bool> visited(n+1);
    visited[1] = true;

    Q.push(1);
    vector<int> D(n+1, MAX_NODES);
    D[1] = 0;
    int dis = 1;
    while (!Q.empty()) {
        int length = Q.size();
        for (int i=0; i<length; i++) {
            int head = Q.front();
            Q.pop();
            for (int v: G[head]) {
                if (!visited[v]) {
                    D[v] = min(D[v], dis);
                    Q.push(v);
                    visited[v] = true;
                }
            }
        }
        dis++;
    }
    if (D[n] == MAX_NODES) {
        return -1;
    }
    return D[n];
}


int main() {
    scanf("%d%d", &n, &m);
    int a, b;
    for (int i=0; i<m; i++) {
        scanf("%d%d", &a, &b);
        if (a != b) {
            G[a].push_back(b);
        }
    }

    int ans = bfs();
    printf("%d\n", ans);
    return 0;
}